package com.yf.tz.controller;

import java.io.IOException;
import java.util.List;

import com.yf.request.SearchParam;
import com.yf.tz.entity.MallItemEntity;
import com.yf.tz.service.MallItemService;
import com.yf.tz.vo.MallOrderVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.annotation.SysLog;
import java.util.Map;
import java.util.stream.Collectors;

import com.yf.consts.DaoConst;
import com.yf.tz.entity.MallOrderEntity;
import com.yf.tz.service.MallOrderService;
import com.yf.response.R;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;


/**
 * 订单Controller
 *
 * @author hxf
 * @email 505860922@qq.com
 * @date Wed Jul 01 16:32:18 CST 2020
 */
@RestController
@RequestMapping("/mall/order")
public class MallOrderController {
    @Autowired
    private MallOrderService mallOrderService;

    @Autowired
    private MallItemService mallItemService;

    /**
     * 列表订单
     */
    @SysLog("查看订单")
    @RequestMapping("/list")
    @RequiresPermissions("mall:order:list")
    public R list(@RequestBody SearchParam<MallOrderVo> params) {
        PageHelper.startPage(params.getPage(),params.getLimit()).setOrderBy(params.getOrder());
        List<MallOrderVo> mallOrderList = mallOrderService.queryListVo(params.getEntity());
        return R.ok(new PageInfo(mallOrderList));
    }

    /**
     * 下载订单
     */
    @SysLog("下载订单")
    @RequestMapping("/down")
    @RequiresPermissions("mall:order:list")
    public void down(@RequestBody SearchParam<MallOrderVo> params, HttpServletResponse response) {
        List<MallOrderVo> mallOrderList = mallOrderService.queryListVo(params.getEntity());
        mallOrderService.downLoad(response,mallOrderList.stream().map(item -> item.getMallOrder()).collect(Collectors.toList()));
    }


    /**
     * 配货
     */
    @SysLog("下载订单")
    @RequestMapping("/down/send")
    @RequiresPermissions("mall:order:list")
    public void downSend( HttpServletResponse response) {
        mallOrderService.downLoadSend(response);
    }

    /**
     * 发货
     */
    @SysLog("上传订单")
    @RequestMapping("/upload")
    @RequiresPermissions("mall:order:list")
    public void upload( MultipartFile file) throws IOException {
        mallOrderService.upload(file.getInputStream());
    }

    /**
     * 重制
     */
    @SysLog("重制订单")
    @RequestMapping("/reset/wait")
    @RequiresPermissions("mall:order:list")
    public R resetWaitSend(){
        mallOrderService.resetWaitSend();
        return R.ok();
    }







    /**
     * 查看订单
     */
    @SysLog("查看订单")
    @RequestMapping("/info/{id}")
    @RequiresPermissions("mall:order:info")
    public R info(@PathVariable("id") Long id) {
        MallOrderVo vo = mallOrderService.getVo(id);
        //获取明细
        List<MallItemEntity> items = mallItemService.findByOrderId(id);
        vo.getMallOrder().setItems(items);
        return R.ok(vo);
    }

    /**
     * 保存订单
     */
    @SysLog("保存订单")
    @RequestMapping("/save")
    @RequiresPermissions("mall:order:save")
    public R save(MallOrderEntity mallOrder) {
        mallOrderService.save(mallOrder);

        return R.ok(mallOrder);
    }

    /**
     * 修改订单
     */
    @SysLog("修改订单")
    @RequestMapping("/update")
    @RequiresPermissions("mall:order:update")
    public R update(@RequestBody MallOrderVo vo) {
        mallOrderService.update(vo.getMallOrder());
        return R.ok(vo);
    }

    /**
     * 删除订单
     */
    @SysLog("删除订单")
    @RequestMapping("/delete")
    @RequiresPermissions("mall:order:delete")
    public R delete(Long[]ids) {
        mallOrderService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 所有订单
     */
    @SysLog("所有订单")
    @RequestMapping("/all")
    public R queryAll (@RequestParam Map<String,Object> params) {

        List<MallOrderEntity> mallOrderList = mallOrderService.queryList(params);

        return R.ok(mallOrderList);
    }

    /***********自定义开始***********/
}
